<%@ page pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.base.DatabaseObject"%>
<%@page import="com.base.Config"%>
<%@page import="java.math.BigDecimal"%>
<%@include file="inc-header.jsp"%><!-- MAIN CONTENT -->
<div class="box bottom10">
	<%@include file="inc-nav-header.jsp"%><%
	String sCatId = Utils.getParam(request, "catid", "0");
	String sPage = Utils.getParam(request, "page", "1");
	String sId = Utils.getParam(request, "id", "0");
	String strAction = Utils.getParam(request, "action", "0");
	DatabaseObject gameCategory = contentDAO.getContentGroup(new BigDecimal(sCatId)); 
%>
	<div class="tab2">
		<%@include file="dichvu-adv.jsp" %> >>
		<a href="story.jsp?act=dv" <%=gameCategory != null?"":"class=\"active\"" %>> Truyện </a>
	</div>
	<!--end .tab2-->
	<!-- BODY CONTENT -->
	<div class="path bottom10">
		<a href="story.jsp?act=dv&type=new"<%="new".equalsIgnoreCase(sType)?" class=\"active\"":"" %>> Mới nhất </a> |
		<a href="story.jsp?act=dv&type=hot"<%="hot".equalsIgnoreCase(sType)?" class=\"active\"":"" %>> Hot nhất </a>
	</div>
	<div>
<%	
	int currPage = Integer.parseInt(sPage);
	int rowsPerPage = 7;
	if (!StringTool.isEmptyOrNul(sCatId)) {
		int totalRecs = contentDAO.countContentByCategoryGroup(new BigDecimal(sCatId));
		int numOfPages = (int)((totalRecs % rowsPerPage != 0) ? (totalRecs / rowsPerPage + 1)
				: (totalRecs / rowsPerPage));
		if (totalRecs <= rowsPerPage)
			numOfPages = 1;
		if (totalRecs == 0)
			numOfPages = 0;
		if (currPage > numOfPages)
			currPage = numOfPages;
		if (currPage <= 0)
			currPage = 1;
		long startPos = rowsPerPage * (currPage - 1) + 1;
		long stopPos = startPos + rowsPerPage - 1;
		if (stopPos > totalRecs)
			stopPos = totalRecs;
		long firstPage = 1; // The first page
		long lastPage = numOfPages; // The last page
		long prevPage = currPage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		long nextPage = currPage + 1;
		if (nextPage > numOfPages) {
			nextPage = numOfPages;
		}
		ArrayList<DatabaseObject> gameList = contentDAO.getContentListByCategoryGroup(new BigDecimal(sCatId), currPage, rowsPerPage);
		
		if (gameList == null || gameList.isEmpty()) {
			if ("new".equalsIgnoreCase(sType)) {
				gameList = contentDAO.getContentListByTypeVn(new BigDecimal("1064"), 1, 8);
			} else if ("hot".equalsIgnoreCase(sType)) {
				gameList = contentDAO.getContentHOTListByTypeVn(new BigDecimal("1064"), 1, 8); 
			}
		}	
		if (gameList != null && !gameList.isEmpty()) {%>
		<table cellpadding="0" cellspacing="0" border="1" width="100%">
		<%
			for (DatabaseObject story : gameList) {%>
			<tr>
			    <td width="100%" style="vertical-align: top; padding: 5px 2px 5px 4px;">
		        <input type="hidden" name="action" value="" />
			    <b><a href="story-detail.jsp?act=dv&catid=<%=story.getAttribute().get("content_group_id") %>&id=<%=story.getAttribute().get("content_id") %>&page=<%=currPage %>" ><strong> <%=story.getAttribute().get("name")%> </strong></a></b>
			    </td>
			</tr>
			<%}%>
		</table>
		<%
		}%>
		<div align="center">
			<%
				long startPage = currPage - 3;
				if (startPage < 1) startPage = 1;
				
				long endPage = startPage + 6;
				if (endPage > numOfPages) {
					endPage = numOfPages;
				}
				if (startPage > 3 && endPage - startPage < 6) startPage = endPage - 6;
				if (startPage > 1) {
	        %>
			<a href="?act=dv&catid=<%=sCatId %>&page=1">Đầu</a>
			<a href="?act=dv&catid=<%=sCatId %>&page=<%=startPage-1 %>"> Trước </a>
			<%}
		        for (long i = startPage; i <= endPage; i++) {
		        	if (currPage == i) {
					out.print("<font style=\"font-weight:bold; font-size: 12px; color: #ff0000;\">" + i + "</font>");
				} else {
	         %>
			<a href="?act=dv&catid=<%=sCatId %>&page=<%=i %>" style="font-size: 12px;"> <%=i%></a>
			<%  }
			}
	        if (endPage < numOfPages)
	        {
		    %>
			<a href="?act=dv&catid=<%=sCatId %>&page=<%=endPage+1 %>" class="style_paging">Tiếp</a>
			<a href="?act=dv&catid=<%=sCatId %>&page=<%=numOfPages %>" class="style_paging">Cuối</a>
			<%}%>
		</div>
		
		<%
	}
	%>
	</div>
	
	<div class="box clearfix">
		<div class="title padbot5px">
			<h3>
				<img src="images/icon-story.png" width="23" height="23" alt="" align="left"> 
				<strong> Danh mục truyện</strong>
			</h3>
		</div>
		<!--end .title-->
		<ul class="list-dot"><%
		String catGameParentId = Config.getStringProperty("CAT_STORY_PARENT_ID", "1088", false);
		if (!StringTool.isEmptyOrNul(catGameParentId)) {
			ArrayList<DatabaseObject> gameCategoryList = contentDAO.getContentGroup(catGameParentId, null);
			for (DatabaseObject storyGroup : gameCategoryList) {%>
			<li>
				<img src="images/icon-list.png" alt="<%=storyGroup.getAttribute().get("NAME") %>">
				<%
				String cate = "1243";
				String cat = storyGroup.getAttribute().get("CONTENT_GROUP_ID").toString();
				if(cat.equals(cate))
				{%>
				<a href="story.jsp?act=dv&catid=<%=storyGroup.getAttribute().get("CONTENT_GROUP_ID") %>"> <%=storyGroup.getAttribute().get("NAME") %> </a>				
				<%}else{
				 %>
				<a href="story-sub.jsp?act=dv&pcatid=<%=storyGroup.getAttribute().get("CONTENT_GROUP_ID") %>"> <%=storyGroup.getAttribute().get("NAME") %> </a>
		<%}}}%>
		</ul>
	</div>
	<!-- END BODY CONTENT -->
</div>
<!--end .box bottom10-->
<%@include file="inc-nav-bottom.jsp"%>
<!-- END MAIN CONTENT -->
<%@include file="inc-footter.jsp"%>
